package cn.wolfcode.wms.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.StringUtils;

import cn.wolfcode.wms.domain.Product;
import cn.wolfcode.wms.query.ProductQueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IProductService;
import cn.wolfcode.wms.util.JsonResult;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.util.UploadUtil;

@Controller
@RequestMapping("/product")
public class ProductController {
	@Autowired
	private IProductService service;
	@Autowired
	private IBrandService brandService;

	@RequestMapping("/list")
	@RequiredPermission("商品列表")
	public String list(Model model, @ModelAttribute("qo") ProductQueryObject qo) {
		model.addAttribute("result", service.query(qo));
		model.addAttribute("brands", brandService.selectAll());
		return "product/list";
	}

	@RequestMapping("/delete")
	@ResponseBody
	@RequiredPermission("商品删除")
	public JsonResult delete(Long id,HttpServletRequest request,String imagePath) {
		JsonResult result = new JsonResult();
		try {
			if (id != null) {
				service.deleteByPrimaryKey(id);
				if (!StringUtils.isEmpty(imagePath)) {
					UploadUtil.deleteFile(request.getServletContext(), imagePath);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("删除失败");
		}
		return result;
	}

	@RequiredPermission("商品编辑")
	@RequestMapping("/input")
	public String input(Long id, Model model) {

		model.addAttribute("brands", brandService.selectAll());
		if (id != null) {
			model.addAttribute("product", service.selectByPrimaryKey(id));
		}
		return "product/input";
	}

	@RequiredPermission("商品保存或更新")
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public JsonResult saveOrUpdate(Product product, MultipartFile pic,HttpServletRequest request) {
		JsonResult result = new JsonResult();
		if (pic!=null) {
			if (!StringUtils.isEmpty(product.getImagePath())) {
				UploadUtil.deleteFile(request.getServletContext(), product.getImagePath());
			}
			String realPath = request.getServletContext().getRealPath("/upload");
			String imagePath = UploadUtil.upload(pic, realPath);
			product.setImagePath(imagePath);
		}
		try {
			if (product.getId() != null) {
				service.updateByPrimaryKey(product);
			} else {
				service.insert(product);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("保存失败");
		}
		return result;
	}
	
	@RequestMapping("/selectProduct")
	public String selectProduct(Model model, @ModelAttribute("qo") ProductQueryObject qo) {
		model.addAttribute("result", service.query(qo));
		model.addAttribute("brands", brandService.selectAll());
		return "product/selectProduct";
	}


}
